package com.yc.snacknet_vue.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.yc.snacknet_vue.bean.GoodsInfo;
import com.yc.snacknet_vue.dao.DBHelper;
import com.yc.snacknet_vue.dao.IGoodsInfoDAO;
import com.yc.snacknet_vue.utils.StringUtil;

/**
 * 商品模型层 实现
 * @author UNO
 * @date 2021年8月9日
 * Email 2759451734@qq.com
 */
public class GoodsInfoDAOImpl implements IGoodsInfoDAO{

	/**
	 * 前台价格区分  查询
	 * @param gname
	 * @param tno
	 * @param sprice
	 * @param eprice
	 * @param page
	 * @param rows
	 * @param order
	 * @return
	 */
	@Override
	public List<GoodsInfo> finds(String gname, String tno, String sprice, String eprice, int page, int rows, String order) {
		DBHelper db = new DBHelper();
		String sql = "select gno, gname, tno, price, balance, unit, qperied, weight, intro, pics pic,descr from goodsinfo where status != 0 ";
		List<Object> params = new ArrayList<Object>();
		//参数判断依据
		if(!StringUtil.checkNull(gname)) {
			sql += " and gname like concat('%',?,'%')";
			params.add(gname);
		}
		if(!StringUtil.checkNull(tno)) {
			sql += " and tno = ?";
			params.add(tno);
		}
		if(!StringUtil.checkNull(sprice)) {
			sql += " and price >= ?";
			params.add(sprice);
		}
		if(!StringUtil.checkNull(eprice)) {
			sql += " and price <= ?";
			params.add(eprice);
		}
		if(!StringUtil.checkNull(order)) {
			sql += " order by price" + order;
		}else {
			sql += " order by gno desc" ;
		}
		sql += " limit ?, ? ";
		params.add((page - 1) * rows);
		params.add(rows);
		if( null == params || params.isEmpty()) {
			return db.findMultiple(GoodsInfo.class, sql);
		}
		return db.findMultiple(GoodsInfo.class, sql,params.toArray());
	}

	//前台价格区间 商品总数
	@Override
	public int total(String gname, String tno, String sprice, String eprice) {
		DBHelper db = new DBHelper();
		String sql = "select count(gno) from goodsinfo where 1 = 1 ";
		List<Object> params = new ArrayList<Object>();
		//参数判断依据
		if(!StringUtil.checkNull(gname)) {
			sql += " and gname like concat('%',?,'%')";
			params.add(gname);
		}
		if(!StringUtil.checkNull(tno)) {
			sql += " and tno = ?";
			params.add(tno);
		}
		if(!StringUtil.checkNull(sprice)) {
			sql += " and price >= ?";
			params.add(sprice);
		}
		if(!StringUtil.checkNull(eprice)) {
			sql += " and price <= ?";
			params.add(eprice);
		}
		if( null == params || params.isEmpty()) {
			return db.getTotal(sql);
		}
		return db.getTotal(sql,params.toArray());
	}

	/**
	 * 查询商品详情
	 * @param gno
	 * @return
	 */
	@Override
	public GoodsInfo findByGno(String gno) {
		DBHelper db = new DBHelper();
		String sql = "select gno, gname, t.tno, price, balance, unit, qperied, weight, intro, pics ,descr "
				+ "from goodsinfo g, goodstype t where g.tno = t.tno and gno = ? ";
		return db.findSingle(GoodsInfo.class, sql, gno);
	}

}
